///////////////////////////////DUMP///////////////////////////////////////

function d(obj,msg,maxDeep) {
	if (!msg) { 
		msg = "___ VARDUMP: ___\n";
		}
	else {
		msg = "___" + msg +":___ \n";
	}
	
	opera.postError(msg + varDump(obj,maxDeep));
	
	function varDump(variable, maxDeep)
	{
		var deep = 0;
		var maxDeep = maxDeep || 0;

		function fetch(object, parent)
		{
			var buffer = '';
			deep++;

			for (var i in object) {
				if (parent) {
					objectPath = parent + '.' + i;
				} else {
					objectPath = i;
				}

				buffer += objectPath + ' (' + typeof object[i] + ')';

				if (typeof object[i] == 'object') {
					buffer += "\n";
					if (deep < maxDeep) {
						buffer += fetch(object[i], objectPath);
					}
				} else if (typeof object[i] == 'function') {
					buffer += "\n";
				} else if (typeof object[i] == 'string') {
					buffer += ': "' + object[i] + "\"\n";
				} else {
					buffer += ': ' + object[i] + "\n";
				}
			}

			deep--;
			return buffer;
		}

		if (typeof variable == 'object') {
			return fetch(variable);
		}

		return '(' + typeof variable + '): ' + variable + "\n";
	}
	
	return obj
}


/**
 *  Shows text in status bar
 * @input txt <string> Text to display
 * @input id  <string> ID of the element inside the status bar to append the text to
 **/
function setStatus(txt, id, attach){
    clearTimeout(timer);
    if (!id) {
		if(attach){
			$('#statusBar #etc').append('<span onClick="$(this).fadeOut(500)" title="Click to hide">' + txt + '</span> ');
		}
		else{
			$('#statusBar #etc')  .html('<span onClick="$(this).fadeOut(500)" title="Click to hide">' + txt + '</span> ');
		}
        var timer = setTimeout(function(){
            $('#statusBar #etc span:first-child').fadeOut(4000)
        }, 10000);
    }
    else {
        $('#statusBar #' + id).html('<span>' + txt + '</span>')
    }
}

/**
 *  Wrapper for widget.showNotification
 */
function notify(txt){
    widget.showNotification(txt, void (null))
}
alert = function(txt){
	widget.showNotification(txt, void (null))
}
function number_format(number, decimals, dec_point, thousands_sep){
    // http://kevin.vanzonneveld.net  
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
    
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
